<extend name="layout/edit" />

<block name="HEADER">
</block>

<block name="CONTENT">
    <control:form id="form"/>
</block>

<block name="FOOTER">
   <script type="text/javascript">
    var  page=(function(ns){
    	
		ns.init = function(){
            //选择活动适用范围
            ns.showGood();
            //选择优惠方式
            ns.showbenefit_mode();

            //选择活动适用监听输入
            form.on('input',"scope",function(res){
                ns.showGood();
            });

            //选择优惠方式监听输入
            form.on('input',"benefit_mode",function(res){
                ns.showbenefit_mode();
            });

        }

		//初始化的时候，选择活动适用范围类型隐藏和显示其他的
        ns.showGood = function(){
            // sections  table-cell
            if(form.getValue('scope')==1){   //1.指定商品适用
                $("#goodGrid").parents('.table-cell').show()
                $("#ShopGrid").parents('.table-cell').hide()
            }else if(form.getValue('scope')==2){       //2.指定商铺分类适用
                $("#goodGrid").parents('.table-cell').hide()
                $("#ShopGrid").parents('.table-cell').show()
            }else{      //代表所有
                $("#goodGrid").parents('.table-cell').hide()
                $("#ShopGrid").parents('.table-cell').hide()
            }
        }

        //选择优惠方式隐藏和显示
        ns.showbenefit_mode = function(){
            if(form.getValue('benefit_mode')==1){   //1.优惠固定金额
                $('#benefit_money').parents('.table-cell').show();
                $('#benefit_discount').parents('.table-cell').hide();
            }else if(form.getValue('benefit_mode')==2){       //2.折扣
                $('#benefit_money').parents('.table-cell').hide();
                $('#benefit_discount').parents('.table-cell').show();
            }
        }

        //为商店增加行
        ns.addShop = function(){
            form.$grids['ShopGrid'].addRow([{
                cate_id:'',
                sort_no:'',
                name:'',

            }]);
        }

        //店铺 - 删除
        ns.deleteShop = function(){
            form.$grids['ShopGrid'].deleteRow();
        }

        //选择店铺
        ns.selectShop = function(){
            var selectedRows = form.$grids['ShopGrid'].getRows();

            var str = "";
            for(var i=0;i<selectedRows.length;i++){
                if(selectedRows[i]['cate_id'])
                str += selectedRows[i]['cate_id']+",";
            }
            if (str.length > 0) {
                str = str.substr(0, str.length - 1);
            }

            var filter = {
                url:"modules/common/selectGoodsCate",
                mode:1,
                type:2,
                id:str
            }
            $.openIframe(filter,"选择店铺",['600px', '480px']);
        }

        ns.selectShopsCallback = function(ids,data){
            form.$grids['ShopGrid'].deleteRow();

            //循环动态填充到grid控件
            for(var i=0;i<data.length;i++){
                form.$grids['ShopGrid'].addRow([{
                    'cate_id':data[i]['id'],
                    'sort_no': data[i]['sort_no'],
                    'name': data[i]['name'],
                }]);
            }

        }


        //商品 - 删除
        ns.deleteGoods = function(){
            form.$grids['goodGrid'].deleteRow();
        }


        /******商品列表********/
        //为商品增加行
        ns.addGoods = function(){
            form.$grids['goodGrid'].addRow([{
                good_id:'',
                goods_no:'',
                good_name:'',
                sale_price:'',
                stock:'',
            }]);
        }


        //选择商品
        ns.selectGoods = function(){

            var selectedRows = form.$grids['goodGrid'].getRows();

            var str = "";
            for(var i=0;i<selectedRows.length;i++){
                if(selectedRows[i]['good_id'])
                str += selectedRows[i]['good_id']+",";
            }
            if (str.length > 0) {
                str = str.substr(0, str.length - 1);
            }

            var filter = {
                url:"modules/common/selectGoodsKC",
                mode:1,
                type:2,
                good_id:str
            }
            $.openIframe(filter,"选择商品",['600px', '480px']);
        }
        ns.selectGoodsCallback = function(ids,data){
            form.$grids['goodGrid'].deleteRow();

            //循环动态填充到grid控件
            for(var i=0;i<data.length;i++){

                form.$grids['goodGrid'].addRow([{
                    'good_id':data[i]['id'],
                    'goods_no':data[i]['goods_no'],
                    'good_name': data[i]['name'],
                    'sale_price': data[i]['sale_price'],
                    'stock': data[i]['stock'],
                }]);

            }

        }
    	
        //保存
        ns.save=function(){   
        	var data = form.getData();
        	if( !data ) {  return false; }

            var goodData = form.$grids['goodGrid'].getRows();
            var shopData = form.$grids['ShopGrid'].getRows();
            if( data.start_time >= data.stop_time ){
				$.alert(0,"开始时间不能大于等于结束时间！");
				return false;
			}

            form.save(4).then(function(res){
                 //异步保存多个商品到优惠券下
                $.ajaxs('/admin/v1/activity_discount/setcoupongs',{
                    coupon_id:res.data,
                    goodData: JSON.stringify(goodData),
                    goodcateDate: JSON.stringify(shopData),
                }).then(function(res){
                    form.opener.grid.load();
                    $.close();
                });

            });
        }
        
        return ns;
    }({}));
    
    $(function(){ page.init(); });
   </script>
</block>



